<!-- 用户管理 -->
<template>
  <div class="userContainer">
    <div v-show="!isShowPublish">
      <div class="top">
        <div class="search">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </div>
        <div class="batch">
          <el-button type="danger" @click="batchDelete" size="mini"
            >审核打回</el-button
          >
        </div>
      </div>
      <el-table
        ref="multipleTable"
        :data="filterTableData"
        style="width: 100%"
        :default-sort="{ prop: 'uid', order: 'descending' }"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="商品名称" sortable prop="goodsname">
        </el-table-column>
        <el-table-column label="商品数量" prop="goodsnum"> </el-table-column>
        <el-table-column label="现价" prop="curprice"> </el-table-column>
        <el-table-column label="原价" prop="preprice"></el-table-column>
        <el-table-column label="封面" prop="coverimage">
          <template slot-scope="scope">
            <img
              width="40"
              height="40"
              :src="getUrl(scope.row.coverimage)"
              alt="avatar.jpeg"
            />
          </template>
        </el-table-column>
        <el-table-column label="详情图片" prop="goodsimage">
          <template slot-scope="scope">
            <div v-for="item in scope.row.goodsimage.split('&')" :key="item">
              <img
                width="40"
                height="40"
                :src="getUrl(item)"
                alt="avatar.jpeg"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              @click="handleDelete(scope.$index, scope.row)"
              type="text"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          small
          background
          layout="prev, pager, next"
          :page-size="10"
          :total="total"
          @current-change="paginationChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  getAllGoodsData,
  batchDeteleGoodsData,
} from "@/utils/api";
import { baseURL } from "@/http";
import { toRaw } from "vue";
export default {
  data() {
    return {
      tableData: [],
      search: "",
      currpage: 1,
      total: 0,
      selectionData: [],
      isShowPublish: false,
      title: "",
      optionType: "",
      selectionObj: {},
      onceEdit: false,
    };
  },
  computed: {
    uid() {
      return sessionStorage.getItem("uid");
    },
  },
  methods: {
    handleDelete(index, row) {
      this.selectionData = [row];
      this.batchDelete();
    },
    async deleteOption() {
      let data = this.selectionData.map((item) => toRaw(item));
      try {
        let message = await batchDeteleGoodsData({
          goods: JSON.stringify(data),
        });
        this.$message.success(message);
        this.request();
        this.clear();
      } catch (err) {
        this.$message.error(err.message);
      }
    },
    handleSelectionChange(selectionData) {
      this.selectionObj[this.currpage] = JSON.parse(
        JSON.stringify(selectionData)
      );
      let data = Object.values(this.selectionObj);
      data = data.filter((item) => item.length !== 0).flat(1);
      this.selectionData = data;
    },
    // 清除选中框
    clearSelection() {
      this.$refs.multipleTable.clearSelection();
    },
    paginationChange(page) {
      console.log(page);
      this.currpage = page;
    },
    async getData(page = 1) {
      let res = await getAllGoodsData({
        page,
      });
      console.log(res);
      this.tableData = res.tableData;
      this.total = res.total;
    },
    getUrl(url) {
      if (!url) return "/img/avatar.jpeg";
      if (url.includes("http")) {
        return url;
      } else {
        return baseURL + url;
      }
    },
    batchUpdate() {
      if (!this.selectionData.length) {
        this.$message.warning("未选择任何数据，无法批量更新");
        return;
      }
      this.isShowModel = true;
      this.title = "更新";
      this.optionType = "update";
    },
    async batchDelete() {
      if (!this.selectionData.length) {
        this.$message.warning("未选择任何数据，无法批量删除");
        return;
      }
      this.deleteOption();
    },
    close() {
      this.isShowPublish = false;
    },
    clear() {
      this.selectionData = [];
      this.$refs.multipleTable.clearSelection();
    },
    request() {
      let page = this.currpage;
      this.getData(page);
    },
  },
  created() {
    this.getData();
  },
  computed: {
    filterTableData() {
      return this.tableData.filter(
        (data) => !this.search || data.username.includes(this.search)
      );
    },
  },
  watch: {
    currpage(newVal) {
      this.getData(newVal);
    },
    tableData() {
      let that = this;
      that.tableData.forEach((item1, index) => {
        const item = toRaw(item1);
        let data = that.selectionData.map((item2) => toRaw(item2));
        data.forEach((item3) => {
          if (item3.uid === item.uid) {
            that.$nextTick(() => {
              that.$refs.multipleTable.toggleRowSelection(item1);
            });
          }
        });
      });
    },
  },
};
</script>

<style lang="less" scoped>
.userContainer {
  background: #fff;
  .top {
    display: flex;
    justify-content: space-between;
    padding: 10px;
    .search {
      width: 200px;
    }
    .batch {
      display: flex;
    }
  }
  .pagination {
    padding: 10px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>